package com.hzjh.h5mall.ui.web

import android.webkit.JavascriptInterface
import com.blankj.utilcode.util.LanguageUtils
import com.hzjh.base.ext.data.UserConfigs
import com.hzjh.base.ext.route.RouteUrl
import com.hzjh.base.ext.utils.ImageSelectUtils
import com.hzjh.base.log.JHLogger
import com.hzjh.base.router.ext.navigationTo
import com.hzjh.base.utils.function.runOnUIThread
import com.hzjh.base.utils.function.toJson
import com.hzjh.base.utils.other.ActivityStackManage
import com.hzjh.base.utils.other.CommUtils
import com.hzjh.base.web.core.JHWebView
import com.hzjh.base.web.core.JsBridgeBase
import com.hzjh.h5mall.BuildConfig
import com.hzjh.h5mall.model.web.JSBridgeModel
import java.util.Locale

class JSBridge(val mWebView: JHWebView) : JsBridgeBase() {
    val viewModel:JSBridgeModel = JSBridgeModel()

    override fun init() {

    }

    @JavascriptInterface
    fun jumpToAppLogin(){
        JHLogger.d("JSBridge调用原生方法 ---- jumpToAppLogin()")
        navigationTo(RouteUrl.LOGIN_ACTIVITY)
    }
    @JavascriptInterface
    fun getAppToken():String{
        JHLogger.d("JSBridge调用原生方法 ---- getAppToken()")
        var userData=""
        if(UserConfigs.isLogin()){
            userData = UserConfigs.getUserData().toJson()
        }
        return userData
    }

    @JavascriptInterface
    fun isAppDebug():Boolean{
        JHLogger.d("JSBridge调用原生方法 ---- isAppDebug()")
        return BuildConfig.DEBUG
    }

    @JavascriptInterface
    fun changeAppLanguage(language: String){
        JHLogger.d("JSBridge调用原生方法 ---- changeAppLanguage($language)")
        val locale = Locale(language)
        LanguageUtils.applyLanguage(locale,true)
    }

    //获取app当前语言
    @JavascriptInterface
    fun getAppLocalLanguage():String{
        val language = LanguageUtils.getAppContextLanguage().language
        JHLogger.d("JSBridge调用H5方法 ---- getAppLocalLanguage() ---- $language")
        return language
    }

    //跳转
    @JavascriptInterface
    fun toAppNamed(routeUrl: String){
        JHLogger.d("JSBridge调用H5方法 ---- toAppNamed($routeUrl)")
        navigationTo(routeUrl)
    }

    //关闭H5页面
    @JavascriptInterface
    fun closeH5(){
        JHLogger.d("JSBridge调用H5方法 ---- close()")
        ActivityStackManage.currentActivity()?.finish()
    }

    //H5选择图片
    @JavascriptInterface
    fun selectAppImg(){
        JHLogger.d("JSBridge调用H5方法 ---- selectAppImg()")
        ImageSelectUtils().showSelectImgDialog {
            //上传图片
            viewModel.uploadImage(it){result->
                JHLogger.d("JSBridge调用H5方法 ---- appImgCallBack(${result?.url})")
                runOnUIThread {
                    mWebView.evaluateJavascript("javascript:window.appImgCallBack('${result?.url}')") {}
                }
            }
        }
    }
    //H5拨打电话
    @JavascriptInterface
    fun callAppPhone(phone: String?){
        JHLogger.d("JSBridge调用H5方法 ---- callAppPhone($phone)")
        CommUtils.callPhone(ActivityStackManage.currentActivity(), phone)
    }
    //H5发送邮箱
    @JavascriptInterface
    fun callAppEmail(email: String?){
        JHLogger.d("JSBridge调用H5方法 ---- callAppEmail($email)")
        CommUtils.sendEmail(ActivityStackManage.currentActivity(), email)
    }


    //登录状态同步
    fun appLoginResult(mWebView: JHWebView,data: Int?){
        JHLogger.d("JSBridge调用H5方法 ---- appLoginResult($data)")
        runOnUIThread {
            mWebView.evaluateJavascript("javascript:window.appLoginResult('$data')") {}
        }
    }

    //Ads订单状态同步
    fun adsStatusChange(mWebView: JHWebView){
        JHLogger.d("JSBridge调用H5方法 ---- adsStatusChange()")
        runOnUIThread {
            mWebView.evaluateJavascript("javascript:window.getadvertisinglist()") {}
        }
    }
}